package com.saikei.demoapp.base

import android.app.Activity
import android.content.Intent
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.os.Bundle
import android.util.Log
import androidx.core.content.ContextCompat.startActivity
import androidx.databinding.ViewDataBinding
import com.saikei.demoapp.BR
import com.saikei.demoapp.util.IntentStarter

abstract class BaseInitVMActivity<VB : ViewDataBinding, VM : BaseInitVM> :
    BaseVMActivity<VB, VM>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewModel.onLoadBundle(intent?.extras)
        bindViewModelAndPage()
        initView()
        initData()
        initObserve()
    }

    abstract fun initView()
    abstract fun initData()
    abstract fun initObserve()

    protected open fun getVmBr(): Int = 0
    protected open fun getPageBr(): Int = 0

    protected open fun bindViewModelAndPage() {
        try {
            mBinding.setVariable(getVmBr(), mViewModel)
            mBinding.setVariable(getPageBr(), this)
            mBinding.lifecycleOwner = this
        } catch (e: Exception) {
            Log.e("BaseInitVMActivity", "绑定失败: ${e.message}", e)
        }
    }

    override fun finish() {
        super.finish()
        IntentStarter.create(this).finish()
    }
}